package com.test.client.customTimeLine;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.UIObject;
import com.netthreads.gwt.simile.timeline.client.TimelineXMLHandler;

public class CustomTimeLine extends JavaScriptObject {

	protected CustomTimeLine() {
		super();
	}

	public static CustomTimeLine create(ArrayList bands, EventSource source, Element divElement, Element clientElement) {
		JavaScriptObject[] bandArr = JavaScriptObjectHelper.listToArray(bands);

		JavaScriptObject jarr = JavaScriptObjectHelper.arrayConvert(bandArr);

		boolean currVisible = UIObject.isVisible(clientElement);
		UIObject.setVisible(clientElement, true);

		CustomTimeLine timeLine = CustomTimeLineImpl.create(jarr, divElement);
		//CustomTimeLineImpl.registerCustomeEventClickHandler();

		UIObject.setVisible(clientElement, currVisible);

		return timeLine;

	}

	/**
	 * Redraw timeline
	 */
	public final void layout() {
		CustomTimeLineImpl.layout(this);
	}

	public final void addOnScrollListener() {
		CustomTimeLineImpl.addScrollListener(0, this);
	}

	public final void clear() {
		CustomTimeLineImpl.clearTimeline(this);
	}

	/**
	 * loadXML through handler function.
	 * 
	 * @param dataUrl
	 * @param handler
	 */
	public final void loadXML(String dataUrl, TimelineXMLHandler handler) {
		CustomTimeLineImpl.loadXML(dataUrl, handler);
	}

	/**
	 * Close info bubble for indicated band
	 * 
	 * @param index
	 */
	public final void closeBubble(int index) {
		CustomTimeLineImpl.closeBubble(index, this);
	}

	public final void renderEvents(ArrayList<TimeLineEvent> timelineEvents) {
		JavaScriptObject array = com.smartgwt.client.util.JSOHelper.arrayConvert(timelineEvents.toArray());
		CustomTimeLineImpl.renderEvents(array, this);
	}

	public final void removeEventsBeforeDate(double date) {
		CustomTimeLineImpl.removeEventsBeforeDate(date, this);
	}

	public final void removeEventsAfterDate(double date) {
		CustomTimeLineImpl.removeEventsAfterDate(date, this);
	}

	public final Duration getCurrentVisibleDuration(int bandIndex) {
		long startDate = (long) CustomTimeLineImpl.getMinVisibleDate(bandIndex, this);
		long endDate = (long) CustomTimeLineImpl.getMaxVisibleDate(bandIndex, this);
		Duration duration = new Duration(new Date(startDate), new Date(endDate));
		return duration;
	}

	public final void setTimeLineBandZeroHeight(int timelineBandZeroHeight) {
		CustomTimeLineImpl.setTimeLineBandZeroHeight(timelineBandZeroHeight, this);

	}
}
